package cn.ffcs.uec.outStaff.action;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.common.util.NodeVoUtil;
import cn.ffcs.uec.common.util.UecConstants;
import cn.ffcs.uec.organization.manager.UecOrgOrgsManager;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uec.outStaff.action.bean.UecOutStaffEditBean;
import cn.ffcs.uec.outStaff.manager.UecOutStaffManager;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;
import cn.ffcs.uom.systemconfig.manager.IdentityCardConfigManager;

@Controller
@Scope("prototype")
public class UecOutStaffEditComposer extends BasePortletComposer {
	private static final long serialVersionUID = -6096343177536382090L;

	private UecOutStaffEditBean bean = new UecOutStaffEditBean();

	private String opType;

	//@Resource
	//private UecOutStaffManager uecOutStaffManager;

	@Resource
	private IdentityCardConfigManager identityCardConfigManager;

	@Resource
	private UecOrgOrgsManager uecOrgOrgsManager;

	private UecOutStaffInfo uecOutStaffInfo;
	private UecOutStaffInfo queryuecOutStaffInfo;
	private UecOutStaffManager uecOutStaffManager = (UecOutStaffManager) ApplicationContextUtil
			.getBean("uecOutStaffManager");
	private UecOrgOrgs uecOrgOrgs;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);

		/**
		 * 1、在UecOutStaffEditComposer中通过addForward来监听UecOrgOrgsBandboxExt里触发的事件！
		 * 2、在onUecOrgOrgsEditSelectedResponse()方法里接收参数
		 */
		this.bean.getUecOrgOrgsBandboxExt().addForward(
				"onUecOrgOrgsEditSelected", this.self,
				"onUecOrgOrgsEditSelectedResponse");

	}

	/**
	 * 监听UecOrgOrgsBandboxExt传出的事件
	 * 
	 * @param event
	 */
	public void onUecOrgOrgsEditSelectedResponse(final ForwardEvent event) {
		if (event.getOrigin().getData() != null) {
			uecOrgOrgs = (UecOrgOrgs) event.getOrigin().getData();
			if (null != uecOrgOrgs) {

			}
		}
	}

	/**
	 * window初始化.
	 * 
	 * @throws Exception
	 *             异常
	 */
	public void onCreate$uecOutStaffEditWin() throws Exception {
		this.bindCombobox();
		opType = (String) arg.get("opType");
		if (opType.equals("add")) {
			bean.getUecOutStaffEditWin().setTitle("外包人员新增");
			uecOutStaffInfo = (UecOutStaffInfo) arg.get("uecOutStaffInfo");
			if (null != uecOutStaffInfo) {
				PubUtil.fillBeanFromPo(uecOutStaffInfo, bean);
				stringTransToDate();
			}
		}
		if (opType.equals("edit")) {
			bean.getUecOutStaffEditWin().setTitle("外包人员信息编辑");
			uecOutStaffInfo = (UecOutStaffInfo) arg.get("uecOutStaffInfo");
			
			if (null != uecOutStaffInfo) {
				PubUtil.fillBeanFromPo(uecOutStaffInfo, bean);
				UecOrgOrgs uecOrgOrgs = UecOrgOrgs.newInstance();
				if (null != uecOutStaffInfo.getPkHrorg()) {
					uecOrgOrgs.setPkOrg(uecOutStaffInfo.getPkHrorg());
					UecOrgOrgs findUecOrgOrgsByUecOrgOrgs = uecOrgOrgsManager
							.findUecOrgOrgsByUecOrgOrgs(uecOrgOrgs);
					if (null != findUecOrgOrgsByUecOrgOrgs) {
						bean.getUecOrgOrgsBandboxExt().setValue(
								findUecOrgOrgsByUecOrgOrgs.getName());
					}
				} else {
					bean.getUecOrgOrgsBandboxExt().setValue(null);
				}
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存.
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = checkUacStaffData();
		if (msg != null) {
			Messagebox.show(msg);
			return;
		}
		if (opType.equals("add")) {
			// 保存员工
			uecOutStaffInfo = UecOutStaffInfo.newInstance();
			PubUtil.fillPoFromEditorBean(uecOutStaffInfo, bean);
			DateFormatter();
			if (null != uecOrgOrgs) {
				if (null != uecOrgOrgs.getPkOrg()) {
					uecOutStaffInfo.setPkHrorg(uecOrgOrgs.getPkOrg());
				}
			}
			uecOutStaffInfo.addOnly();
			Events.postEvent("onOK", this.self, uecOutStaffInfo);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if (opType.equals("edit")) {
			PubUtil.fillPoFromEditorBean(uecOutStaffInfo, bean);
			DateFormatter();
			if (null != uecOrgOrgs) {
				if (null != uecOrgOrgs.getPkOrg()) {
					uecOutStaffInfo.setPkHrorg(uecOrgOrgs.getPkOrg());
				}
			}
			String mobtelePhone="";
			if(!StrUtil.isNullOrEmpty(bean.getMobtelePhone().getValue())) {
				mobtelePhone=bean.getMobtelePhone().getValue();
			}
			List<UecOutStaffInfo> list= null;
			if(!StrUtil.isNullOrEmpty(mobtelePhone)) {
				queryuecOutStaffInfo=UecOutStaffInfo.newInstance();
				queryuecOutStaffInfo.setMobtelePhone(mobtelePhone);
				list= uecOutStaffManager.findUecOutStaffInfoList(queryuecOutStaffInfo);
			}
			boolean flag=false;
			if(list !=null && list.size()>0) {
				for(UecOutStaffInfo uOutStaffInfo:list) {
					if(!uOutStaffInfo.getOutStaffId().equals(uecOutStaffInfo.getOutStaffId())) {
						flag=true;
						break;
					}
				}
			}
			if(flag) {
				Messagebox.show("手机号已有人使用，请联系管理员核实！！");
			}else {
				// 更新数据
				uecOutStaffInfo.updateOnly();
				Events.postEvent("onOK", this.self, uecOutStaffInfo);
				onCancel();
				Messagebox.show("数据修改成功！");
			}
		}
	}

	/**
	 * 取消.
	 */
	public void onCancel() {
		bean.getUecOutStaffEditWin().onClose();
	}

	/**
	 * 绑定下拉框 绑定combobox
	 */
	private void bindCombobox() {
		// 绑定启用状态
		List<NodeVo> nodeVoEnableStateList = new ArrayList<NodeVo>();
		String[][] enableState = { { "1", "未启用" }, { "2", "已启用" },
				{ "3", "已停用" } };
		for (int i = 0; i < enableState.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(enableState[i][j]);
				nodeVo.setName(enableState[i][j + 1]);
			}
			nodeVoEnableStateList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getEnableState(),
				nodeVoEnableStateList);

		// 绑定性别sex:
		List<NodeVo> nodeVoSexList = new ArrayList<NodeVo>();
		String[][] sex = { { "1", "男" }, { "2", "女" } };
		for (int i = 0; i < sex.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(sex[i][j]);
				nodeVo.setName(sex[i][j + 1]);
			}
			nodeVoSexList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getSex(), nodeVoSexList);

		List<NodeVo> nodeVoIsList = new ArrayList<NodeVo>();
		String[][] is = { { "Y", "是" }, { "N", "否" } };
		for (int i = 0; i < is.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(is[i][j]);
				nodeVo.setName(is[i][j + 1]);
			}
			nodeVoIsList.add(nodeVo);
		}
		// 是否装维
		ListboxUtils.rendererForEdit(bean.getIsZW(), nodeVoIsList);
		// 是否渠道
		ListboxUtils.rendererForEdit(bean.getIsQD(), nodeVoIsList);

		List<NodeVo> nodeVoIsSendList = new ArrayList<NodeVo>();
		String[][] isSend = { { "1", "是" }, { "0", "否" } };
		for (int i = 0; i < isSend.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(isSend[i][j]);
				nodeVo.setName(isSend[i][j + 1]);
			}
			nodeVoIsSendList.add(nodeVo);
		}
		// 是否发送
		ListboxUtils.rendererForEdit(bean.getIsSend(), nodeVoIsSendList);
		// 是否发送
		ListboxUtils.rendererForEdit(bean.getDr(), nodeVoIsSendList);
		// 全日制学历
		ListboxUtils.rendererForEdit(bean.getFulltimeEducation(),
				NodeVoUtil.getDictionaries(UecConstants.ACADEMIC_DEGREE));
		// 最高学历
		ListboxUtils.rendererForEdit(bean.getHighestEducation(),
				NodeVoUtil.getDictionaries(UecConstants.ACADEMIC_DEGREE));
		// 学位
		ListboxUtils.rendererForEdit(bean.getDegree(),
				NodeVoUtil.getDictionaries(UecConstants.ACADEMIC_DEGREE));
		// 绑定民族nationality
		ListboxUtils.rendererForEdit(bean.getNationality(),
				NodeVoUtil.getDictionaries(UecConstants.NATION));
		// 婚姻状况marital
		ListboxUtils.rendererForEdit(bean.getMarital(),
				NodeVoUtil.getDictionaries(UecConstants.MARRIAGE));
		// 健康状况
		ListboxUtils.rendererForEdit(bean.getHealth(),
				NodeVoUtil.getDictionaries(UecConstants.HELATH));
		// 政治面貌
		//ListboxUtils.rendererForEdit(bean.getPolity(),
		//		NodeVoUtil.getDictionaries(UecConstants.POLITICAL_OUTLOOK));
		// 户口性质characterrpr
		ListboxUtils.rendererForEdit(bean.getCharacterrpr(),
				NodeVoUtil.getDictionaries(UecConstants.NATURE_REGISTRATION));
		// 进入电信系统来源--glbdef2
		ListboxUtils.rendererForEdit(bean.getGlbdef2(), NodeVoUtil
				.getDictionaries(UecConstants.ASSCESS_SOURCE_TELESTSTEM));

		// 是否是历史成员
		List<NodeVo> nodeVoIshiskeypsnList = new ArrayList<NodeVo>();
		String[][] ishiskeypsn = { { "Y", "是" }, { "N", "否" } };
		for (int i = 0; i < ishiskeypsn.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(ishiskeypsn[i][j]);
				nodeVo.setName(ishiskeypsn[i][j + 1]);
			}
			nodeVoIshiskeypsnList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getIshiskeypsn(),
				nodeVoIshiskeypsnList);
		// 是否是门店营业员
		ListboxUtils.rendererForEdit(bean.getIsshopassist(),
				nodeVoIshiskeypsnList);
		// 是否承包经营个人
		ListboxUtils.rendererForEdit(bean.getGlbdef14(), nodeVoIshiskeypsnList);
		// 证件类型

		List<NodeVo> nodeVoIdTYpeList = new ArrayList<NodeVo>();
		String[][] idtype = { { "0", "身份证" }, { "1", "军官证" },{ "2", "护照" }, { "3", "香港身份证" },{ "4", "回乡证" },
				{ "5", "台湾身份证" },{ "6", "澳门身份证" }, { "7", "台胞证" }, { "8", "外国人永久居留证" } };
		for (int i = 0; i < idtype.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(idtype[i][j]);
				nodeVo.setName(idtype[i][j + 1]);
			}
			nodeVoIdTYpeList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getIdtype(),
				nodeVoIdTYpeList);
	}

	/**
	 * 校验
	 * 
	 * @return
	 */
	private String checkUacStaffData() {
		// BaseInfo Check
		if (StrUtil.isNullOrEmpty(bean.getPsnName().getValue())) {
			return "请填写人员名";
		} else if (StrUtil.checkBlank(bean.getPsnName().getValue())) {
			return "人员名中有空格";
		} else if (StrUtil.isNullOrEmpty(bean.getIdNum().getValue())) {
			return "请填写证件号码";
		} else if (StrUtil.checkBlank(bean.getIdNum().getValue())) {
			return "证件号码中有空格";
		}

		return null;
	}

	/**
	 * 将Date类型的日期转换成String类型
	 */
	public void DateFormatter() {
		// 出生日期
	//	uecOutStaffInfo.setBirthDate(DateUtil.dateToStr(bean.getBirthDate()
	//			.getValue()));
		// 参加工作日期
		uecOutStaffInfo.setJoinWorkDate(DateUtil.dateToStr(bean
				.getJoinWorkDate().getValue()));
		// 进入电信系统日期
		uecOutStaffInfo.setGlbdef3(DateUtil.dateToStr(bean.getGlbdef3()
				.getValue()));
		// 进入本单位日期
		uecOutStaffInfo.setEnterUnitTime(DateUtil.dateToStr(bean
				.getEnterUnitTime().getValue()));
		// 入党(团)日期
		//uecOutStaffInfo.setJoinPolityDate(DateUtil.dateToStr(bean
		//		.getJoinPolityDate().getValue()));
		// 预计退休日期
		//uecOutStaffInfo.setGlbdef15(DateUtil.dateToStr(bean.getGlbdef15()
		//		.getValue()));
		// 离退休日期
		//uecOutStaffInfo.setRetireDate(DateUtil.dateToStr(bean.getRetireDate()
		//		.getValue()));
		// 身故日期
		uecOutStaffInfo.setDieDate(DateUtil.dateToStr(bean.getDieDate()
				.getValue()));
	}

	/**
	 * 将String类型的日期转换成Date类型
	 */
	public void stringTransToDate() throws ParseException {
		// 出生日期
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getBirthDate())) {
			bean.getBirthDate().setValue(
					DateUtil.patternStrToDate(uecOutStaffInfo.getBirthDate()));
		}
		// 参加工作日期
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getJoinWorkDate())) {
			bean.getJoinWorkDate()
					.setValue(
							DateUtil.patternStrToDate(uecOutStaffInfo
									.getJoinWorkDate()));
		}
		// 进入电信系统日期
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getGlbdef3())) {
			bean.getGlbdef3().setValue(
					DateUtil.patternStrToDate(uecOutStaffInfo.getGlbdef3()));
		}
		// 进入本单位日期
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getEnterUnitTime())) {
			bean.getEnterUnitTime().setValue(
					DateUtil.patternStrToDate(uecOutStaffInfo
							.getEnterUnitTime()));
		}
		// 入党(团)日期
		/*if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getJoinPolityDate())) {
			bean.getJoinPolityDate().setValue(
					DateUtil.patternStrToDate(uecOutStaffInfo
							.getJoinPolityDate()));
		}*/
		// 预计退休日期
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getGlbdef15())) {
			bean.getGlbdef15().setValue(
					DateUtil.patternStrToDate(uecOutStaffInfo.getGlbdef15()));
		}
		// 离退休日期
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getRetireDate())) {
			bean.getRetireDate().setValue(
					DateUtil.patternStrToDate(uecOutStaffInfo.getRetireDate()));
		}
		// 身故日期
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getDieDate())) {
			bean.getDieDate().setValue(
					DateUtil.patternStrToDate(uecOutStaffInfo.getDieDate()));
		}
	}

}
